<?php

namespace App\Http\Controllers\Seller;

use App\Http\Controllers\Controller;
use App\Models\AdminEstate;
use App\Services\SmsService;
use App\Services\StoreClassService;
use App\Services\UploadService;
use Illuminate\Http\Request;
use App\Services\UserService;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;

class LoginController extends Controller
{
    /**
     * @param UserService $user_service
     * @return array
     */
    public function login(UserService $user_service): array
    {
        $info = $user_service->login('phone');
        return $info['status'] ? $this->success($info['data']) : $this->error($info['msg']);
    }

    /**
     * 检测是否登陆
     * @param UserService $user_service
     * @return array
     */
    public function check_login(UserService $user_service): array
    {
        $info = $user_service->checkLogin('user', true);
        return $info['status'] ? $this->success($info['data']) : $this->error($info['msg']);
    }

    /**
     * 退出账号
     * @return array
     */
    public function logout(): array
    {
        try {
            auth('user')->logout();
        } catch (\Exception $e) {
            return $this->success([], __('base.success'));
        }
        return $this->success([], __('base.success'));
    }

    // 注册
    public function register(): array
    {
        $user_service = new UserService();
        $rs = $user_service->register();
        return $rs['status'] ? $this->success($rs['data'], $rs['msg']) : $this->error($rs['msg']);
    }

    /**
     * 找回密码
     * @return array
     */
    public function forget_password(): array
    {
        $user_service = new UserService();
        $rs = $user_service->forgetPassword('phone');
        return $rs['status'] ? $this->success($rs['data'], $rs['msg']) : $this->error($rs['msg']);
    }

    /**
     * 发送短信
     * @param Request $request
     * @return array
     */
    public function send_sms(Request $request): array
    {
        $sms_service = new SmsService();
        $rs = $sms_service->sendSms($request->phone, $request->name);
        return $rs['status'] ? $this->success($rs['data'], $rs['msg']) : $this->error($rs['msg']);
    }

    /**
     * Notes: 修改用户信息
     * User: lstink
     * Date: 2021/1/29
     * Time: 6:05 下午
     * @param Request $request
     * @param AdminEstate $admin_estate
     */
    public function changeInfo(Request $request, UserService $userService)
    {
        $username = $request->post('username', '');
        $password = $request->post('password', '');
        //获取用户信息
        $user_info = $request->user();
        if (!empty($username)) {
            $user_info->username = $username;
        }
        if (!empty($password)) {
            $user_info->password = Hash::make($password);
        }
        $user_info->save();
        //获取用户信息
        $user_info = $userService->getUserInfo('seller');
        return $this->success($user_info, __('base.success'));
    }

    // 图片上传
    public function store_join_upload(Request $request, UploadService $upload_service): ?array
    {
        $store_id = $this->get_store(true);
        $name = $request->name ?? '';
        $rs = $upload_service->store_join($store_id);
        if ($rs['status']) {
            return $this->success(['url' => $rs['data'], 'name' => $name], $rs['msg']);
        } else {
            return $this->error($rs['msg']);
        }
    }

    /**
     * 获取数据成功
     * @return array
     */
    public function getData()
    {
        $community = DB::table('community')->where(['status' => 1])->select()->get();
        $cate = DB::table('shop_stores_cate')->where(['status' => 1])->select()->get();
        return $this->success(['cate' => $cate, 'community' => $community], '获取成功');
    }
}
